/**
 * 出入库列表inAndOut.js
 * 版本号： v1.0.0
 */
$(function () {

    //设置时间
    $("#stoStartTime").datebox({
        onSelect : function(beginDate){
            $('#stoEndTime').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<=date;
                }
            });
        }
    });

    var lastChecked = 0;
    var stId = $('#stId').val();
    $("#inAndOutList_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        striped: true,
        pageSize: 10,
        pageNumber: 1,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/store/inAndOutList.do',
        queryParams:{stId:stId},
        idField: 'stoId',
        columns: [
            [
                {field: 'stoPerson', title: '人员', width: 80, fixed: true},
                {field: 'stoType', title: '类型', width: 80, fixed: true,
                    formatter: function (value) {
                        if (value == "stoIn") {
                            return "入库";
                        }
                        if (value == "stoOut") {
                            return "出库";
                        }
                    }},
                {
                    field: 'storeWay', title: '入库方式', width: 80,fixed: true,
                    formatter: function (value) {
                        if (value == "1") {
                            return "流程入库";
                        }
                        if (value == "2") {
                            return "填写入库";
                        }
                    }
                },
                {field: 'stoPurpose', title: '出库用途', width: 120, fixed: true},
                {field: 'stoNumber', title: '数量', width: 120, fixed: true},
                {field: 'stoKcNumber', title: '当前库存数量', width: 120, fixed: true},
                {field: 'stoDate', title: '时间', width: 120}
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#inAndOutList_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#inAndOutList_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#inAndOutList_dataList").datagrid("selectRow", 0);
            }
        }
    });


    //定义可操作按钮
    $("#inAndOutList_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });


});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id: btnOptions[i].value.meId,
                        text: btnOptions[i].value.name,
                        iconCls: btnOptions[i].value.iconCls,
                        handler: function () {
                            store_loadSearch();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 *  加载查询
 */
function store_loadSearch() {
    //刷新dg页码
    refreshPageNumber("inAndOutList_dataList");
    //查询数据
    $("#inAndOutList_dataList").datagrid("reload", {
        stId: $("#stId").val(),
        stoType: $("#stoType").combobox("getValue"),
        stoPerson: $("#stoPerson").textbox("getValue"),
        stoStartTime: $("#stoStartTime").datebox("getValue"),
        stoEndTime: $("#stoEndTime").datebox("getValue"),
        storeWay: $("#storeWay").combobox("getValue")
    });
}